package com.kdtech.analyse.NewsPaper;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.StringUtils;
import com.kdtech.analyse.AnalyseNews;
/**
 * http://jnsb1.e23.cn/ 济南报业
 * @author Persh 
 */
public class JnbyNewsPaperAnalyse implements AnalyseNews {

	
	
	public boolean isDetailPage(String url) {
		boolean bRet = false;
		String[] regex = {
				"http://jnsb.e23.cn/shtml/jnsb/[0-9]{8}/[0-9]*.shtml",
				"http://jnrb.e23.cn/shtml/jinrb/[0-9]{8}/[0-9]*.shtml",
				"http://dsnb.e23.cn/shtml/dsnb/[0-9]{8}/[0-9]*.shtml",
				"http://jkb.e23.cn/shtml/ddjkb/[0-9]{8}/[0-9]*.shtml",
				"http://rkb.e23.cn/shtml/rkdb/[0-9]{8}/[0-9]*.shtml"
				};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {		
		NewsMeta newspaper = new NewsMeta();
				
		String html = urlMeta.getHtml();
		String url = urlMeta.getUrl();		
			
		String title = null;
		String content = null;
		Long date = null;
		Document doc = Jsoup.parse(html);
		
		
		newspaper.setUrl(url);		
		if (url.startsWith("http://jnsb.e23.cn/shtml/jnsb/")){
			title = doc.select("h2").text();
			content =HtmlCleaner.getContentHtml(url,doc.select("div.article"));
			date=DateUtils.matchDate(doc.select("p.p3").text());
		}
		if (url.startsWith("http://jnrb.e23.cn/shtml/jinrb/")){
			title = doc.select("h2").text();
			content =HtmlCleaner.getContentHtml(url,doc.select("div.article"));
			date=DateUtils.matchDate(doc.select("p.p3").text());
		}
		if (url.startsWith("http://dsnb.e23.cn/shtml/dsnb/")){
			title = doc.select("h2").text();
			content =HtmlCleaner.getContentHtml(url,doc.select("div.article"));
			date=DateUtils.matchDate(doc.select("p.p3").text());
		}
		if (url.startsWith("http://jkb.e23.cn/shtml/ddjkb/")){
			title = doc.select("h2").text();
			content =HtmlCleaner.getContentHtml(url,doc.select("div.article"));
			date=DateUtils.matchDate(doc.select("p.p3").text());
		}
		if (url.startsWith("http://rkb.e23.cn/shtml/rkdb/")){
			title = doc.select("h2").text();
			content =HtmlCleaner.getContentHtml(url,doc.select("div.article"));
			date=DateUtils.matchDate(doc.select("p.p3").text());
		}
		
		if(date==null){
			date=DateUtils.matchDate(url);
		}
		if(StringUtils.isBlank(content)){
			content =HtmlCleaner.getContentHtml(url,doc.select("div#copytext"));
		}
		
		newspaper.setTitle(StringUtils.trimSpace(title));
		newspaper.setContent(StringUtils.trimSpace(content));
		newspaper.setDate(date);

		return newspaper;
	}
	
	
	public boolean isTaskPage(String url) {
		boolean bRet = false;
		String[] regex = {
				"http://jnsb.e23.cn/shtml/jnsb/[yyyy][MM][dd]/v[A-Z]*[0-9]*.shtml",
				"http://jnrb.e23.cn/shtml/jinrb/[yyyy][MM][dd]/v[A-Z]?[0-9]*.shtml",
				"http://dsnb.e23.cn/shtml/dsnb/[yyyy][MM][dd]/v[A-Z]?[0-9]*.shtml",
				"http://jkb.e23.cn/shtml/ddjkb/[yyyy][MM][dd]/v[A-Z]*[0-9]*.shtml",
				"http://rkb.e23.cn/shtml/rkdb/[yyyy][MM][dd]/v[A-Z]*[0-9]*.shtml"
				};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}
	
	

	

	
	
}
